{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "91b229b0",
   "metadata": {},
   "source": [
    "# Numpy 中arg运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ddffbb71",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5ebf419f",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.normal(0, 1, 1000000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4859392a",
   "metadata": {},
   "source": [
    "## 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "add4ea4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "39195"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmin(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a6e8a26",
   "metadata": {},
   "source": [
    "上面是求最小数的索引位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "94897c8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-4.818129906502869"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[39195]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "54439b59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-4.818129906502869"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f8c3111e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "892222"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29324852",
   "metadata": {},
   "source": [
    "## 排序和使用索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "56cad5b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.arange(16)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5187da17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([13,  3,  5,  6,  1,  4,  0,  2, 15, 10, 11, 14,  7, 12,  8,  9])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.shuffle(x)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f9076d96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "982e8f40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([13,  3,  5,  6,  1,  4,  0,  2, 15, 10, 11, 14,  7, 12,  8,  9])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ae0e4f0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c966fb8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a3091ca4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 4, 3],\n",
       "       [5, 6, 1, 0],\n",
       "       [1, 8, 5, 7],\n",
       "       [7, 8, 3, 5]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.random.randint(10, size=(4,4))\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "801986a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 1, 0],\n",
       "       [1, 6, 3, 3],\n",
       "       [5, 8, 4, 5],\n",
       "       [7, 8, 5, 7]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(X, axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73ead433",
   "metadata": {},
   "source": [
    "上面是针对列进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "784a7ceb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4],\n",
       "       [0, 1, 5, 6],\n",
       "       [1, 5, 7, 8],\n",
       "       [3, 5, 7, 8]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(X, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6e3ba90",
   "metadata": {},
   "source": [
    "上面针对行进行排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21e8d752",
   "metadata": {},
   "source": [
    "### 使用索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "57eec48a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a7bea361",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.shuffle(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e4a27a9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4,  0,  8,  9,  5,  2,  7,  6, 11, 14, 13,  1, 10,  3, 12, 15])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "49a322f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 11,  5, 13,  0,  4,  7,  6,  2,  3, 12,  8, 14, 10,  9, 15],\n",
       "      dtype=int64)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea32e0b2",
   "metadata": {},
   "source": [
    "上面将数据进行排序，返回排序之后原始数据的序号数组，数据最小的是序号为1的，其次是序号11的，以此类推"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a0b4ec22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  0,  2,  3,  4,  5,  6,  7, 11, 14, 13,  9, 10,  8, 12, 15])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.partition(x, 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "199cf6fe",
   "metadata": {},
   "source": [
    "指定分区数，5表示前面5个数是一个分区，剩下的是另外一个分区，且后面分区的数比前面分区的数大，每个分区的数顺序是乱序的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "cee1dda4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  2,  5,  8,  1, 12, 14])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.array([3,2,5,8,1, 12, 14])\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ef8b795d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  5,  8, 12, 14])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.partition(d, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "876f2af1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11,  1,  5, 13,  0,  4,  7,  6,  8,  9, 10,  3, 12,  2, 14, 15],\n",
       "      dtype=int64)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = np.argpartition(x, 3)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08836877",
   "metadata": {},
   "source": [
    "指定分区排序返回索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1d2030b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  0,  2,  3,  4,  5,  6,  7, 11, 14, 13,  9, 10,  8, 12, 15])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.partition(x, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "709368c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  0,  2,  3,  4,  5,  6,  7, 11, 14, 13,  9, 10,  8, 12, 15])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[s]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9785a02e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 4, 3],\n",
       "       [5, 6, 1, 0],\n",
       "       [1, 8, 5, 7],\n",
       "       [7, 8, 3, 5]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4302eb2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 3, 2],\n",
       "       [3, 2, 0, 1],\n",
       "       [0, 2, 3, 1],\n",
       "       [2, 3, 0, 1]], dtype=int64)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(X, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "255bf651",
   "metadata": {},
   "source": [
    "列的方向按行进行排序索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "10ef6f68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 3, 2],\n",
       "       [3, 2, 0, 1],\n",
       "       [0, 2, 3, 1],\n",
       "       [2, 3, 0, 1]], dtype=int64)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argpartition(X, 2, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1586ad1",
   "metadata": {},
   "source": [
    "列的方向按行进行分区索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c1e9628",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
